package com.shengdong.water.code.router

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.result.ActivityResultLauncher
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavigationCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.shengdong.water.WaterApp
import com.shengdong.water.code.config.RouterConfig
import java.io.Serializable

/**
 * 路由管理类
 */
object RouterManager {

    /**
     * 初始化路由
     */
    fun init(isDebug: Boolean) {
        if (isDebug) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(WaterApp.instance)
    }

    /**
     * 构建路由
     */
    fun build(path: String): RouterBuilder {
        return RouterBuilder(path)
    }

    /**
     * 直接导航到指定路由
     */
    fun navigate(path: String) {
        ARouter.getInstance().build(path).navigation()
    }

    /**
     * 路由构建器
     */
    @Suppress("UNCHECKED_CAST")
    class RouterBuilder(private val path: String) {
        private val postcard: Postcard = ARouter.getInstance().build(path)

        /**
         * 添加整型参数
         */
        fun withInt(key: String, value: Int): RouterBuilder {
            postcard.withInt(key, value)
            return this
        }

        /**
         * 添加长整型参数
         */
        fun withLong(key: String, value: Long): RouterBuilder {
            postcard.withLong(key, value)
            return this
        }

        /**
         * 添加字符串参数
         */
        fun withString(key: String, value: String?): RouterBuilder {
            postcard.withString(key, value)
            return this
        }

        fun withFlags(flags: Int): RouterBuilder {
            postcard.withFlags(flags)
            return this
        }

        /**
         * 添加布尔参数
         */
        fun withBoolean(key: String, value: Boolean): RouterBuilder {
            postcard.withBoolean(key, value)
            return this
        }

        /**
         * 添加序列化参数
         */
        fun withSerializable(key: String, value: Serializable?): RouterBuilder {
            postcard.withSerializable(key, value)
            return this
        }

        /**
         * 添加Parcelable参数
         */
        fun withParcelable(key: String, value: Parcelable?): RouterBuilder {
            postcard.withParcelable(key, value)
            return this
        }

        /**
         * 添加Bundle参数
         */
        fun withBundle(key: String, value: Bundle?): RouterBuilder {
            postcard.withBundle(key, value)
            return this
        }

        /**
         * 添加转场动画
         */
        fun withTransition(enterAnim: Int, exitAnim: Int): RouterBuilder {
            postcard.withTransition(enterAnim, exitAnim)
            return this
        }

        /**
         * 导航
         */
        fun navigate() {
            postcard.navigation()
        }

        /**
         * 导航并获取结果
         */
        fun navigate(activity: Activity, requestCode: Int) {
            postcard.navigation(activity, requestCode)
        }

        /**
         * 导航并监听结果
         */
        fun navigate(activity: Activity, requestCode: Int,callback: NavigationCallback) {
            postcard.navigation(activity, requestCode, callback)
        }

        /**
         * 导航并获取目标实例
         */
        fun <T> navigation(): T? {
            return ARouter.getInstance().build(path).navigation() as? T
        }

    }

    /**
     * 注入参数
     */
    fun inject(target: Any) {
        ARouter.getInstance().inject(target)
    }


    /**

     * 导航到指定路径，并清除任务栈
     */

    fun navigateWithClearTask(path: String) {
        build(path)
            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            .navigate()

    }



    /**
     * 导航到登录页面，并清除任务栈
     */
    fun navigateToLogin(redirectPath: String? = null) {
        val builder = build(RouterConfig.LOGIN_PATH)
            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
            .withBoolean("FROM_401", true)
        if (!redirectPath.isNullOrEmpty()) {
            builder.withString("REDIRECT_PATH", redirectPath)
        }
        builder.navigate()
    }

}